package controller

import (
	"gin/response"
	"gin/service"
	"gin/validate"
	"github.com/gin-gonic/gin"
)

func ExcelFormatPasteData(ctx *gin.Context) {

	var selectDictQuery validate.ExcelFormatPasteData

	jsonErr := ctx.ShouldBindJSON(&selectDictQuery)

	if jsonErr != nil {
		response.Fail(ctx, nil, jsonErr.Error())
		return
	}
	res := service.ExcelFormatPasteData(selectDictQuery.Parsed, selectDictQuery.HasHeader == "yes", selectDictQuery.ModuleCode)
	response.Success(ctx, gin.H{"data": res}, "success")
}

// 格式化excel文件数据
func ExcelFormatFileData(ctx *gin.Context) {
	fileImport, e := ctx.FormFile("file")
	if e != nil {
		response.Fail(ctx, nil, e.Error())
		return
	}
	var ExcelFormatFileData validate.ExcelFormatFileData

	jsonErr := ctx.Bind(&ExcelFormatFileData)

	if jsonErr != nil {
		response.Fail(ctx, nil, jsonErr.Error())
		return
	}

	res, err := service.ExcelFormatFileData(fileImport, ExcelFormatFileData)
	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}
	response.Success(ctx, gin.H{"data": res}, "success")
	return
}

// excel导入数据
func ExcelImportData(ctx *gin.Context) {

	var excelImportData validate.ExcelImportData

	jsonErr := ctx.ShouldBindJSON(&excelImportData)

	if jsonErr != nil {
		response.Fail(ctx, nil, jsonErr.Error())
		return
	}

	res := service.ExcelImportData(excelImportData)

	response.Success(ctx, gin.H{"data": res}, "success")
	return
}
